/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jeesite.modules.st.config.Constant;
import com.jeesite.modules.st.entity.StCustomRecord;
import com.jeesite.modules.st.entity.StMaterialRecord;
import com.jeesite.modules.st.entity.StMaterialType;
import com.jeesite.modules.st.service.StCustomRecordService;
import com.jeesite.modules.st.service.StMaterialRecordService;
import com.jeesite.modules.st.service.StMaterialTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.st.entity.StProductRecord;
import com.jeesite.modules.st.service.StProductRecordService;

import java.util.List;

/**
 * 产品档案Controller
 *
 * @author HOHD
 * @version 2020-12-23
 */
@Controller
@RequestMapping(value = "${adminPath}/st/stProductRecord")
public class StProductRecordController extends BaseController {

    @Autowired
    private StProductRecordService stProductRecordService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public StProductRecord get(String id, boolean isNewRecord) {
        return stProductRecordService.get(id, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("st:stProductRecord:view")
    @RequestMapping(value = {"list", ""})
    public String list(StProductRecord stProductRecord, Model model) {
        model.addAttribute("stProductRecord", stProductRecord);
        return "modules/st/stProductRecordList";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("st:stProductRecord:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<StProductRecord> listData(StProductRecord stProductRecord, HttpServletRequest request, HttpServletResponse response) {
        stProductRecord.setPage(new Page<>(request, response));
        Page<StProductRecord> page = stProductRecordService.findPage(stProductRecord);
        return page;
    }

    @Autowired
    StMaterialRecordService stMaterialRecordService;
    ObjectMapper mapper = new ObjectMapper();
    @Autowired
    StMaterialTypeService stMaterialTypeService;
    @Autowired
    StCustomRecordService stCustomRecordService;
    /**
     * 查看编辑表单
     */
    @RequiresPermissions("st:stProductRecord:view")
    @RequestMapping(value = "form")
    public String form(StProductRecord stProductRecord, Model model) throws JsonProcessingException {
        List<StMaterialRecord> materialRecordList = stMaterialRecordService.findList(new StMaterialRecord());
        model.addAttribute("materialList", mapper.writeValueAsString(materialRecordList));
        StMaterialType entity = new StMaterialType();
        entity.setBelong(Constant.st_material_type_belong.complete);
        List<StMaterialType> listMaterialType = stMaterialTypeService.findList(entity);
        model.addAttribute("listMaterialType", listMaterialType);
        List<StCustomRecord> listCustomRecord = stCustomRecordService.findList(new StCustomRecord());
        model.addAttribute("listCustomRecord", listCustomRecord);
        model.addAttribute("stProductRecord", stProductRecord);
        return "modules/st/stProductRecordForm";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("st:stProductRecord:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated StProductRecord stProductRecord) {
        stProductRecordService.save(stProductRecord);
        return renderResult(Global.TRUE, text("保存产品档案成功！"));
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("st:stProductRecord:edit")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(StProductRecord stProductRecord) {
        stProductRecordService.delete(stProductRecord);
        return renderResult(Global.TRUE, text("删除产品档案成功！"));
    }

}